<template>
	<view>
		<view class="main">
			<u-form :model="form" ref="uForm" label-width="160">
				<u-form-item label="头像">
					<u-avatar :src="form.avatar" size="120"></u-avatar>
				</u-form-item>
				<u-form-item label="昵称">
					<u-input v-model="form.nickName"></u-input>
				</u-form-item>
				<u-form-item label="性别">
					<u-radio-group v-model="radio" @change="change">
						<u-radio v-for="(item, index) in radioList" :key="index" :name="item.name"
							:disabled="item.disabled">
							{{ item.name }}
						</u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item label="用户名">
					<u-input v-model="form.userName" disabled></u-input>
				</u-form-item>
				<u-form-item label="手机号">
					<u-input v-model="form.phonenumber"></u-input>
				</u-form-item>
				<u-form-item label="身份证">
					<u-input v-model="form.idCard" disabled></u-input>
				</u-form-item>
				<u-form-item label="邮箱">
					<u-input v-model="form.email" disabled></u-input>
				</u-form-item>
			</u-form>
		</view>
		<button class="bu" style="margin-top: 40rpx;" @click="submit">修改信息</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					userName: '',
					avatar: '',
					sex: '1',
					nickName: '',
					email: '',
					phonenumber: '',
					idCard: ''
				},
				radio: '男',
				radioList: [{
						name: '男',
						disabled: false
					},
					{
						name: '女',
						disabled: false
					}
				]
			};
		},
		onLoad() {
			this.getUser()
		},
		methods: {
			change(e) {
				this.radio = e
				if (this.radio === '男') {
					this.form.sex = '1'
				} else {
					this.form.sex = '0'
				}
			},
			submit() {
				this.$request({
					url: '/prod-api/api/common/user',
					method: 'PUT',
					data: {
						...this.form
					}
				}).then(res => {
					if (res.code === 200) {
						uni.showToast({
							title: '修改成功'
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/user/user'
							})
						}, 1000)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'error'
						})
					}
				})
			},
			getUser(){
				this.$request({
					url: '/prod-api/api/common/user/getInfo'
				}).then(res => {
					this.form = res.user
					if(this.form.sex === '1'){
						this.radio = '男'
					}else{
						this.radio = '女'
					}
					this.form.idCard = this.form.idCard.replace(/\w{2}-\w*-\w{4}/, '$1************$2')
				})
			}
		}
	}
</script>

<style lang="scss">
	view {
		padding: 20rpx;

		.main {
			padding: 20rpx;
		}
	}
</style>